package com.meida.student.app.common.controller;

import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.meida.common.mybatis.model.ResultBody;
import com.meida.common.utils.DateUtils;
import com.meida.common.utils.RedisUtils;
import com.meida.module.bus.provider.service.BusInternationalCodeService;
import com.meida.module.bus.provider.utils.HtmlToImageUtil;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.thymeleaf.TemplateEngine;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * 通用接口
 *
 * @author gp
 */
@Slf4j
@Controller
public class ApiCommonController {
    @Autowired
    private BusInternationalCodeService busInternationalCodeService;

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private TemplateEngine templateEngine;

    @ApiOperation(value = "国际区号-列表", notes = "国际区号列表")
    @ResponseBody
    @GetMapping(value = "/common/getInternationalCodeList")
    public ResultBody list(@RequestParam(required = false) Map params) {
        return busInternationalCodeService.listEntityMap(params);
    }

    @ApiOperation(value = "获取配置", notes = "获取配置")
    @ResponseBody
    @GetMapping(value = "/common/getSysConfig")
    public ResultBody getSysConfig(@RequestParam(required = false) Map params) {
        return busInternationalCodeService.listEntityMap(params);
    }

    @GetMapping(value = "/common/shareHtml")
    public String shareHtml(ModelMap model) {
        model.put("desc", "新年快乐\uD83E\uDD73\uD83E\uDD73\uD83D\uDE0F这个推送精准投放深得我心 已经在卸载了=");
        model.put("img", "https://upload-images.jianshu.io/upload_images/912344-3054132dd6939004.png?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240");
        return "share";
    }

    @ApiOperation(value = "测试分享图片", notes = "测试分享图片")
    @GetMapping(value = "/common/testShareImg")
    public void test(HttpServletResponse response) {
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("msg", "html--生成图片测试新年快乐\uD83E\uDD73\uD83E\uDD73\uD83D\uDE0F");
            map.put("img", "https://upload-images.jianshu.io/upload_images/912344-3054132dd6939004.png?imageMogr2/auto-orient/strip|imageView2/1/w/300/h/240");
        } catch (Exception e) {
            log.error("异常", e.getMessage());
        }
    }


    /**
     * image浏览器下载
     */
    @GetMapping(value = "/common/image/downloadshare")
    @ApiOperation(value = "image浏览器下载")
    public void imageDownloadShare(HttpServletResponse response) {
        Map<String, Object> variables = new HashMap<>(4);
        variables.put("desc", "新年快乐\uD83E\uDD73\uD83E\uDD73\uD83D\uDE0F这个推送精准投放深得我心 已经在卸载了=）");
        HtmlToImageUtil.download(templateEngine, "share", variables, response, IdWorker.getIdStr() + ".jpg");
    }

    public static void main(String[] args) {
        String dateTimeStr = "2022-03-27 02:30:51";
        Date date = DateUtils.strToDate(dateTimeStr, "yyyy-MM-dd HH:mm:ss");
        String s = DateUtils.formatFromTodayByTargetTimezone(date,"UTC-5");
        System.out.println(s);
    }
}
